import { useCasinoStore, useCurrencyStore, useWalletStore } from '@/store'
import { SOCKET_EVENTS, SocketMessage } from '@/constants'
import { PatchWalletEnum } from '@/types'

// TODO nickson 提出使用bus的方式,在具体页面里去监听,解耦各个模块
const SocketEventsMap = <Record<SOCKET_EVENTS, (data: SocketMessage['data']) => void>>{
	[SOCKET_EVENTS.USER_INFO]: () => {},
	[SOCKET_EVENTS.WALLET_BALANCE]: (data) => {
		const walletStore = useWalletStore()
		walletStore.patchWallet(PatchWalletEnum.balanceItem, data)
	},
	[SOCKET_EVENTS.EXCHANGE_RATE]: (data) => {
		const currencyStore = useCurrencyStore()
		currencyStore.setExchangeRate(data)
	},
	[SOCKET_EVENTS.PAYOUT_RANK_DAILY]: () => {},
	[SOCKET_EVENTS.PAYOUT_RANK_WEEKLY]: () => {},
	[SOCKET_EVENTS.PAYOUT_RANK_MONTHLY]: () => {},
	[SOCKET_EVENTS.BET_RANK_MINE_TOP_50]: () => {},
	[SOCKET_EVENTS.BET_RANK_ALL_TOP_50]: (data) => {
		const casinoStore = useCasinoStore()
		casinoStore.setAllBetList(data)
	},
	[SOCKET_EVENTS.BET_RANK_BOLD_TOP_50]: () => {},
	[SOCKET_EVENTS.WEEKLY_CONTEST]: (data) => {
		const casinoStore = useCasinoStore()
		casinoStore.setWeeklyRace(data)
	}
}

export const messageHandler = (message: SocketMessage) => {
	const { command, data } = message
	SocketEventsMap[command]?.(data)
	// const appStore = useAppStore()
	// const slotStore = useSlotStore()
	// const casinoStore = useCasinoStore()
	// const matchStore = useMatchStore()
	// const walletStore = useWalletStore()
	// switch (command) {
	// 	case SOCKET_APP_VERSION:
	// 		// store.commit('app/setVersion', data)
	// 		break
	// 	case SOCKET_SUB_EVENTS:
	// 		if (data.locale && data.locale !== appStore.lang) {
	// 			return
	// 		}
	// 		matchStore.updateSocket(data)
	// 		bus.$emit(SOCKET_SUB_EVENTS, data)
	// 		break
	// 	case SOCKET_EVENTS.WALLET_BALANCE:
	// 		walletStore.setBalance(data)
	// 		break
	// 	case SOCKET_CONTEST_UP_POINTS:
	// 		// store.commit('contest/updatePoints', data)
	// 		break
	// 	case SOCKET_ASYNC_ORDER:
	// 		// store.commit('betting/updateAsyncOrder', data)
	// 		break
	// 	case SOCKET_FEED_SETTLED:
	// 		bus.$emit(SOCKET_FEED_SETTLED, data)
	// 		break
	// 	case SOCKET_EVENT_CHANGE:
	// 		bus.$emit(SOCKET_EVENT_CHANGE, data)
	// 		break
	// 	// case SOCKET_SLOT_LIVE_WINS:
	// 	//   store.commit('slots/setWinnerList', { type: 0, data })
	// 	//   break
	// 	case SOCKET_SLOT_LIVE_WINS_D:
	// 		// console.log('SOCKET_SLOT_LIVE_WINS_D', data)
	// 		// store.commit('slots/setWinnerList', { type: 1, data })
	// 		slotStore.setLiveWins({ type: 1, data })
	// 		break
	// 	case SOCKET_SLOT_LIVE_WINS_W:
	// 		// console.log('SOCKET_SLOT_LIVE_WINS_W', data)
	// 		// store.commit('slots/setWinnerList', { type: 2, data })
	// 		slotStore.setLiveWins({ type: 2, data })
	// 		break
	// 	case SOCKET_SLOT_LIVE_WINS_M:
	// 		// console.log('SOCKET_SLOT_LIVE_WINS_M', data)
	// 		// store.commit('slots/setWinnerList', { type: 3, data })
	// 		slotStore.setLiveWins({ type: 3, data })
	// 		break
	// 	case SOCKET_SLOT_MARQUEE:
	// 		// console.log('SOCKET_SLOT_MARQUEE', data)
	// 		slotStore.addLiveBigWin(data)
	// 		// store.commit('slots/addLiveBigDataList', data)
	// 		break
	// 	case SOCKET_SLOT_GAME_UPDATE:
	// 		// console.log('SOCKET_SLOT_GAME_UPDATE', data)
	// 		slotStore.updateAllListStatus(data)
	// 		break
	// 	case SOCKET_CASINO_LIVE_WINS_D:
	// 		console.log('SOCKET_CASINO_LIVE_WINS_D', data)
	// 		casinoStore.setLiveWins({ index: 1, data })
	// 		break
	// 	case SOCKET_CASINO_LIVE_WINS_W:
	// 		console.log('SOCKET_CASINO_LIVE_WINS_W', data)
	// 		casinoStore.setLiveWins({ index: 2, data })
	// 		break
	// 	case SOCKET_CASINO_LIVE_WINS_M:
	// 		console.log('SOCKET_CASINO_LIVE_WINS_M', data)
	// 		casinoStore.setLiveWins({ index: 3, data })
	// 		break
	// 	case SOCKET_CASINO_MARQUEE:
	// 		console.log('SOCKET_CASINO_MARQUEE', data)
	// 		casinoStore.addLiveBigWin(data)
	// 		break
	// 	case SOCKET_CASINO_GAME_UPDATE:
	// 		console.log('SOCKET_CASINO_GAME_UPDATE', data)
	// 		casinoStore.updateAllListStatus(data)
	// 		break
	// 	case SOCKET_RACE_RANKING:
	// 		// console.log('SOCKET_RACE_RANKING', data)
	// 		bus.$emit(SOCKET_RACE_RANKING, data)
	// 		break
	// 	case SOCKET_LIVE_BIG_WIN_SPORTS:
	// 		console.log('SOCKET_LIVE_BIG_WIN_SPORTS', data)
	// 		bus.$emit(SOCKET_LIVE_BIG_WIN_SPORTS, data)
	// 		break
	// }
}

// export const auth = (data: any) => {
// 	sendSocket(SOCKET_AUTH, data)
// }
//
// export const setOdds = (data: any) => {
// 	sendSocket(SOCKET_SET_ODDS, data)
// }
//
// export const subscribeEvents = (data: any) => {
// 	sendSocket(SOCKET_SUB_EVENTS, data)
// }
//
// export const unSubscribeEvents = (data: any) => {
// 	sendSocket(SOCKET_UNSUB_EVENTS, data)
// }
//
// export const subSlotEvents = (data: any) => {
// 	sendSocket(SOCKET_SUB_SLOTS_EVENTS, data)
// }
//
// export const unSubSlotEvents = (data: any) => {
// 	sendSocket(SOCKET_UNSUB_SLOTS_EVENTS, data)
// }
//
// export const subChannels = (data: any) => {
// 	sendSocket(SOCKET_SUB_SLOTS_EVENTS, data)
// }
//
// export const unSubChannels = (data: any) => {
// 	sendSocket(SOCKET_UNSUB_SLOTS_EVENTS, data)
// }
